﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace UpdateTool
{
    public partial class Form1 : Form
    {
        Form1 form1;
        public Form1()
        {
            this.form1 = this;
            InitializeComponent();
            this.Width = 635;
            this.Height = 58;
            moreVisible(false);//控制高级参数的可见标志
        }
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        private bool beginMove = false;//初始化鼠标位置
        private int currentXPosition;
        private int currentYPosition;
        private IniFiles iniFiles = new IniFiles(Application.StartupPath + @"\Config.ini");

        private void button_Quit_Click(object sender, EventArgs e)
        {
            Application.ExitThread();//关闭软件1
            Application.Exit();//关闭软件2
            System.Diagnostics.Process.GetCurrentProcess().Kill();//关闭软件3

            //Application.ExitThread();//重启软件1
            //Application.Exit();//重启软件2
            //Application.Restart();//重启软件3
            //System.Diagnostics.Process.GetCurrentProcess().Kill();//重启软件4
        }

        private void button_Hide_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal && this.Visible == true)
            {
                this.notifyIcon1.Visible = true;//在通知区显示Form的Icon
                this.WindowState = FormWindowState.Minimized;
                this.Visible = false;
                this.ShowInTaskbar = false;//使Form不在任务栏上显示
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt + F4)时 发生          
            {
                e.Cancel = true;
                this.ShowInTaskbar = false;
                this.notifyIcon1.Icon = this.Icon;
                this.Hide();
            }
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Show();
            }

            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
            }
            
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.ExitThread();//关闭软件1
            Application.Exit();//关闭软件2
            System.Diagnostics.Process.GetCurrentProcess().Kill();//关闭软件3
        }

        private void mouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                beginMove = true;
                currentXPosition = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标
                currentYPosition = MousePosition.Y;//鼠标的y坐标为当前窗体左上角y坐标
            }
        }
        private void mouseMove()
        {
            if (beginMove)
            {
                this.Left += MousePosition.X - currentXPosition;//根据鼠标x坐标确定窗体的左边坐标x
                this.Top += MousePosition.Y - currentYPosition;//根据鼠标的y坐标窗体的顶部，即Y坐标
                currentXPosition = MousePosition.X;
                currentYPosition = MousePosition.Y;
            }
        }
        private void mouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentXPosition = 0; //设置初始状态
                currentYPosition = 0;
                beginMove = false;
            }
        }
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown(e);
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            mouseMove();
        }
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseUp(e);
        }
        private void label_Name_En_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown(e);
        }
        private void label_Name_En_MouseMove(object sender, MouseEventArgs e)
        {
            mouseMove();
        }
        private void label_Name_En_MouseUp(object sender, MouseEventArgs e)
        {
            mouseUp(e);
        }
        private void label_Name_Cn_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown(e);
        }
        private void label_Name_Cn_MouseMove(object sender, MouseEventArgs e)
        {
            mouseMove();
        }
        private void label_Name_Cn_MouseUp(object sender, MouseEventArgs e)
        {
            mouseUp(e);
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            string comStartup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);// 获取全局 开始 文件夹位置
            string filename = comStartup + @"\UpdateTool.lnk";
            // 1、首先判断文件或者文件路径是否存在
            if (File.Exists(filename))
            {
                checkBox_BootRun.Checked = true;
            }
            else
            {
                checkBox_BootRun.Checked = false;
            }
            readAllRecord();

            refreshButton();

            Thread thread = new Thread(threadRunning);
            thread.IsBackground = true;//设置线程为后台线程,那样进程里就不会有未关闭的程序了
            thread.Start();
        }

        private void moreVisible(bool visibleFlag)//控制高级参数的可见标志
        {
            checkBox_BootRun.Visible = visibleFlag;
            checkBox_OpenProcess.Visible = visibleFlag;
            checkBox_BackupFirst.Visible = visibleFlag;
            textBox_LinkTime.Visible = visibleFlag;
            textBox_CheckTime.Visible = visibleFlag;
            dateTimePicker_UpdateTime.Visible = visibleFlag;
            button_Insert.Visible = visibleFlag;
            button_Modify.Visible = visibleFlag;
            button_Delete.Visible = visibleFlag;
            dataGridView1.Visible = visibleFlag;
        }

        private void pictureBox_More_Click(object sender, EventArgs e)
        {
            if (this.Height == 58)
            {
                this.Height = 437;
                moreVisible(true);//控制高级参数的可见标志
            }
            else
            {
                this.Height = 58;
                moreVisible(false);//控制高级参数的可见标志
            }
        }

        private void pictureBox_More_DoubleClick(object sender, EventArgs e)
        {
            if (this.Height == 58)
            {
                this.Height = 437;
                moreVisible(true);//控制高级参数的可见标志
            }
            else
            {
                this.Height = 58;
                moreVisible(false);//控制高级参数的可见标志
            }
        }

        /// <summary>
        /// 写入一条运行参数
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="local"></param>
        /// <param name="cloud"></param>
        /// <param name="processcheck"></param>
        /// <param name="updatemethod"></param>
        private void writeOneRecord(string filename, string local, string cloud, bool processcheck, int updatemethod)
        {
            iniFiles.WriteString("Local", filename, local);
            iniFiles.WriteString("Cloud", filename, cloud);
            iniFiles.WriteString("ProcessCheck", filename, processcheck.ToString());
            iniFiles.WriteString("UpdateMethod", filename, updatemethod.ToString());
        }

        /// <summary>
        /// 写入一条运行参数
        /// </summary>
        /// <param name="filename"></param>
        private void writeOneRecord(string filename)
        {
            bool f = iniFiles.WriteString("Local", filename, null);
            f = iniFiles.WriteString("Cloud", filename, null);
            f = iniFiles.WriteString("ProcessCheck", filename, null);
            f = iniFiles.WriteString("UpdateMethod", filename, null);
        }

        /// <summary>
        /// 将全部软件运行参数，写入ini配置文件
        /// </summary>
        private void writeRunParam()
        {
            iniFiles.WriteString("RunParam", "checkBox_OpenProcess", checkBox_OpenProcess.Checked.ToString());
            iniFiles.WriteString("RunParam", "checkBox_BackupFirst", checkBox_BackupFirst.Checked.ToString());
            iniFiles.WriteString("RunParam", "textBox_LinkTime", textBox_LinkTime.Text);
            iniFiles.WriteString("RunParam", "textBox_CheckTime", textBox_CheckTime.Text);
            int hour = this.dateTimePicker_UpdateTime.Value.Hour;
            int minute = this.dateTimePicker_UpdateTime.Value.Minute;
            int second = this.dateTimePicker_UpdateTime.Value.Second;
            string t = hour.ToString().PadLeft(2, '0') + ":" + minute.ToString().PadLeft(2, '0') + ":" + second.ToString().PadLeft(2, '0');
            //DateTime dateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, second);
            iniFiles.WriteString("RunParam", "dateTimePicker_UpdateTime", t);
            
        }

        public string[] comboBox_datasource = new string[] { "0.在指定时间点，强行升级", "1.检测到关闭时，自动升级", "2.弹窗由管理员，手工升级" };
        private void readAllRecord()
        {
            bool haveDataFlag = false;
            if (iniFiles.ExistINIFile())
            {
                string checkBox_OpenProcess_Str = iniFiles.ReadString("RunParam", "checkBox_OpenProcess");
                try { checkBox_OpenProcess.Checked = Convert.ToBoolean(checkBox_OpenProcess_Str); } catch { }

                string checkBox_BackupFirst_Str = iniFiles.ReadString("RunParam", "checkBox_BackupFirst");
                try { checkBox_BackupFirst.Checked = Convert.ToBoolean(checkBox_BackupFirst_Str); } catch { }

                string textBox_LinkTime_Str = iniFiles.ReadString("RunParam", "textBox_LinkTime");
                try { int i = Convert.ToInt32(textBox_LinkTime_Str); textBox_LinkTime.Text = i.ToString(); } catch { }

                string textBox_CheckTime_Str = iniFiles.ReadString("RunParam", "textBox_CheckTime");
                try { int i = Convert.ToInt32(textBox_CheckTime_Str); textBox_CheckTime.Text = i.ToString(); } catch { }

                string dateTimePicker_UpdateTime_Str = iniFiles.ReadString("RunParam", "dateTimePicker_UpdateTime");
                try
                {
                    if (dateTimePicker_UpdateTime_Str.Length == 8)
                    {
                        string hh = dateTimePicker_UpdateTime_Str.Substring(0, 2);
                        string mm = dateTimePicker_UpdateTime_Str.Substring(3, 2);
                        string ss = dateTimePicker_UpdateTime_Str.Substring(6, 2);
                        int hour = Convert.ToInt32(hh);
                        int minute = Convert.ToInt32(mm);
                        int second = Convert.ToInt32(ss);
                        DateTime dateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, second);
                        dateTimePicker_UpdateTime.Value = dateTime;
                    }
                    
                }
                catch { }
                string[] array = iniFiles.ReadIniAllKeys("Local");
                for (int i = 0; i < array.Length; i++)
                {
                    Console.WriteLine("行号=" + i + "，数组=" + array[i]);
                    int index = array[i].IndexOf("=", StringComparison.CurrentCultureIgnoreCase);
                    string filename = array[i].Substring(0, index);
                    string local = iniFiles.ReadString("Local", filename);
                    string cloud = iniFiles.ReadString("Cloud", filename);
                    string processcheck = iniFiles.ReadString("ProcessCheck", filename);
                    string updatemethod = iniFiles.ReadString("UpdateMethod", filename);
                    dataGridView1_AddOneLine(filename, local, cloud, processcheck, updatemethod);
                    haveDataFlag = true;
                }
            }
            if (haveDataFlag)
            {
                dataGridView1.Rows[0].Selected = true;
            }
        }

        private void dataGridView1_AddOneLine(string filename, string local, string cloud, string processcheck, string updatemethod)
        {
            DataGridViewRow row = new DataGridViewRow();

            DataGridViewTextBoxCell cell_textbox_filename = new DataGridViewTextBoxCell();
            cell_textbox_filename.Value = filename;
            row.Cells.Add(cell_textbox_filename);

            DataGridViewTextBoxCell cell_textbox_local = new DataGridViewTextBoxCell();
            cell_textbox_local.Value = local;
            row.Cells.Add(cell_textbox_local);

            DataGridViewTextBoxCell cell_textbox_cloud = new DataGridViewTextBoxCell();
            cell_textbox_cloud.Value = cloud;
            row.Cells.Add(cell_textbox_cloud);

            DataGridViewCheckBoxCell cell_checkbox_processcheck = new DataGridViewCheckBoxCell();
            cell_checkbox_processcheck.Value = Convert.ToBoolean(processcheck);
            row.Cells.Add(cell_checkbox_processcheck);


            DataGridViewComboBoxCell cell_combobox_updatemethod = new DataGridViewComboBoxCell();
            cell_combobox_updatemethod.DataSource = comboBox_datasource;
            int updatemethod_Int = Convert.ToInt32(updatemethod);
            if (updatemethod_Int < 0 || updatemethod_Int >= comboBox_datasource.Length)
            {
                updatemethod_Int = 0;
            }
            cell_combobox_updatemethod.Value = comboBox_datasource[Convert.ToInt32(updatemethod)];
            row.Cells.Add(cell_combobox_updatemethod);
            //
            //cell_button_delete.UseColumnTextForButtonValue = true;
            dataGridView1.Rows.Add(row);
        }

        private void dataGridView1_ModifyOneLine(string filename, string local, string cloud, string processcheck, string updatemethod, int index)
        {
            DataGridViewTextBoxCell cell_textbox_local = new DataGridViewTextBoxCell();
            cell_textbox_local.Value = local;
            dataGridView1.Rows[index].Cells[1] = cell_textbox_local;

            DataGridViewTextBoxCell cell_textbox_cloud = new DataGridViewTextBoxCell();
            cell_textbox_cloud.Value = cloud;
            dataGridView1.Rows[index].Cells[2] = cell_textbox_cloud;

            DataGridViewCheckBoxCell cell_checkbox_processcheck = new DataGridViewCheckBoxCell();
            cell_checkbox_processcheck.Value = Convert.ToBoolean(processcheck);
            dataGridView1.Rows[index].Cells[3] = cell_checkbox_processcheck;


            DataGridViewComboBoxCell cell_combobox_updatemethod = new DataGridViewComboBoxCell();
            cell_combobox_updatemethod.DataSource = comboBox_datasource;
            int updatemethod_Int = Convert.ToInt32(updatemethod);
            if (updatemethod_Int < 0 || updatemethod_Int >= comboBox_datasource.Length)
            {
                updatemethod_Int = 0;
            }
            cell_combobox_updatemethod.Value = comboBox_datasource[Convert.ToInt32(updatemethod)];
            dataGridView1.Rows[index].Cells[4] = cell_combobox_updatemethod;
            
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
            if (e.Button == System.Windows.Forms.MouseButtons.Right || e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                //dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
                dataGridView1.Rows[e.RowIndex].Selected = true;
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            refreshButton();
        }

        private void refreshButton()
        {
            if (dataGridView1.Rows.Count > 0)
            {
                button_Check.Enabled = true;
                button_Update.Enabled = true;
            }
            else
            {
                button_Check.Enabled = false;
                button_Update.Enabled = false;
            }
        }

        /// <summary>
        /// 修改了一个已存在的参数，需要保存并刷新显示
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="local">本地文件地址</param>
        /// <param name="cloud">云端文件地址</param>
        /// <param name="processcheck">进程检测标志</param>
        /// <param name="updatemethod">更新方法</param>
        /// <param name="index">序号</param>
        public void saveDataGridViewChange(string filename, string local, string cloud, bool processcheck, int updatemethod, int index)
        {
            if (index >= dataGridView1.Rows.Count) return;//不存在需要修改的行，则退出
            if (filename.CompareTo(dataGridView1.Rows[index].Cells[0].Value.ToString()) != 0) return;//文件名与之前的不相同，则退出
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((index != i) && (filename.CompareTo(dataGridView1.Rows[i].Cells[0].Value.ToString()) == 0))
                    return;//文件名与其他的文件名相同，则退出
            }
            dataGridView1_ModifyOneLine(filename, local, cloud, processcheck.ToString(), updatemethod.ToString(), index);
            writeOneRecord(filename, local, cloud, processcheck, updatemethod);
        }

        /// <summary>
        /// 创建了一个新的参数，需要保存并刷新显示
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="local">本地文件地址</param>
        /// <param name="cloud">云端文件地址</param>
        /// <param name="processcheck">进程检测标志</param>
        /// <param name="updatemethod">更新方法</param>
        public void saveDataGridViewChange(string filename, string local, string cloud, bool processcheck, int updatemethod)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (filename.CompareTo(dataGridView1.Rows[i].Cells[0].Value.ToString()) == 0)
                    return;//文件名与其他的文件名相同，则退出
            }
            dataGridView1_AddOneLine(filename, local, cloud, processcheck.ToString(), updatemethod.ToString());
            writeOneRecord(filename, local, cloud, processcheck, updatemethod);
        }

        /// <summary>
        /// 修改一条监控记录，按钮被按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Modify_Click(object sender, EventArgs e)
        {
            string filename = "";
            string local = "";
            string cloud = "";
            string processcheck = "";
            string updatemethod = "";
            int index = 0;
            bool selectFlag = false;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Selected == true)
                {
                    filename = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    local = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    cloud = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    processcheck = dataGridView1.Rows[i].Cells[3].Value.ToString();
                    updatemethod = dataGridView1.Rows[i].Cells[4].Value.ToString().Substring(0, 1);
                    selectFlag = true;
                    index = i;
                    break;
                    //writeOneRecord(str[i]);
                    //DataGridViewRow row = dataGridView1.Rows[i];
                    //dataGridView1.Rows.Remove(row);
                }
            }
            if (selectFlag)
            {
                Form_More form_More = new Form_More(form1, filename, local, cloud, 
                    Convert.ToBoolean(processcheck), Convert.ToInt32(updatemethod), index);
                form_More.Show();
            }
        }

        /// <summary>
        /// 插入一条监控记录，按钮被按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Insert_Click(object sender, EventArgs e)
        {
            Form_More form_More = new Form_More(form1);
            form_More.Show();
        }

        /// <summary>
        /// 删除一条监控记录，按钮被按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Delete_Click(object sender, EventArgs e)
        {
            string[] str = new string[dataGridView1.Rows.Count];
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Selected == true)
                {
                    str[i] = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    writeOneRecord(str[i]);
                    DataGridViewRow row = dataGridView1.Rows[i];
                    dataGridView1.Rows.Remove(row);
                }
            }
        }

        /// <summary>
        /// 联网检查，按钮被按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Check_Click(object sender, EventArgs e)
        {
            string filename = "";
            string local = "";
            string cloud = "";
            int index = 0;
            bool haveSelectFlag = false;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Selected == true)
                {
                    filename = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    local = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    cloud = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    index = i;
                    haveSelectFlag = true;
                    break;
                }
            }
            if (haveSelectFlag && (cloud.Length > 0))
            {
                Download d = new Download();
                
                Console.WriteLine("Check Cloud. Have New File = " + d.GetInfo(cloud) + ". cloud=" + cloud);
            }
            
        }

        /// <summary>
        /// 创建快捷方式，在使用 WshShell 时，需要添加引用，从 Com 中搜索：Windows Script Host Object Model 添加即可
        /// </summary>
        /// <param name="directory">在哪个目录下创建快捷方式</param>
        /// <param name="shortcutName">创建的快捷方式名称</param>
        /// <param name="targetPath">链接的目录</param>
        /// <param name="description">描述</param>
        /// <param name="iconLocation">快捷方式图标</param>
        /// <returns>bool 指示创建是否成功</returns>
        public static bool Create(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null)
        {
            try
            {
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                //添加引用 Com 中搜索 Windows Script Host Object Model
                string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName));
                IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
                IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);//创建快捷方式对象
                shortcut.TargetPath = targetPath;//指定目标路径
                shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);//设置起始位置
                shortcut.WindowStyle = 1;//设置运行方式，默认为常规窗口
                shortcut.Description = description;//设置备注
                shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//设置图标路径
                shortcut.Save();//保存快捷方式

                return true;
            }
            catch
            { }
            return false;
        }
        
        /// <summary>
        /// 开机自启动，复选框变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox_BootRun_CheckedChanged(object sender, EventArgs e)
        {
            //Environment.GetFolderPath(Environment.SpecialFolder.Startup);// 获取当前登录用户的 开始 文件夹位置
            string comStartup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);// 获取全局 开始 文件夹位置
            string filename = comStartup + @"\UpdateTool.lnk";
            if (checkBox_BootRun.Checked)//被选中
            {
                if (!File.Exists(filename))
                {
                    Create(comStartup, "UpdateTool", Application.ExecutablePath);
                }
            }
            else
            {
                if (File.Exists(filename))// 1、首先判断文件或者文件路径是否存在
                {
                    FileAttributes attr = File.GetAttributes(filename);// 2、根据路径字符串判断是文件还是文件夹
                    if (attr == FileAttributes.Directory)// 3、根据具体类型进行删除
                    {
                        Directory.Delete(filename, true);// 3.1、删除文件夹
                    }
                    else
                    {
                        File.Delete(filename);// 3.2、删除文件
                    }
                    //File.Delete(filename);
                }
            }
        }

        /// <summary>
        /// 监控异常退出的进程自动重开，复选框变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox_OpenProcess_CheckedChanged(object sender, EventArgs e)
        {
            writeRunParam();
        }

        /// <summary>
        /// 更新前先备份，复选框变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox_BackupFirst_CheckedChanged(object sender, EventArgs e)
        {
            writeRunParam();
        }

        /// <summary>
        /// 时间设置窗口，变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dateTimePicker_UpdateTime_ValueChanged(object sender, EventArgs e)
        {
            writeRunParam();
        }

        /// <summary>
        /// 进程检测频率，秒数变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_CheckTime_TextChanged(object sender, EventArgs e)
        {
            writeRunParam();
        }

        /// <summary>
        /// 联网检查频率，小时数变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_LinkTime_TextChanged(object sender, EventArgs e)
        {
            writeRunParam();
        }

        /* 串口通信的事务处理线程。顺序执行和循环执行串口发送、等待接收、超时判断、接收解码。*/
        private void threadRunning()//串口通信的事务处理线程
        {
            while (true)
            {
                int sleeptime = 5;
                try { Convert.ToInt32(textBox_CheckTime.Text); } catch { }
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    bool flag_processcheck = Convert.ToBoolean(dataGridView1.Rows[i].Cells[3].Value.ToString());
                    if (flag_processcheck)//进程检测标志
                    {
                        string filename = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    }
                }
                Thread.Sleep(sleeptime * 1000);
            }
        }
    }
}
